package sk.ogame.realm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * @author 支绍昆
 * 2023/2/14
 **/
@Configuration
public class RestTemplateConfig {

    public String cookie = "S36_cookietime=315360000; uid=67510; S36_sid=REgE87; " +
            "S36_auth=e4fdGw0oje8iyouF21xY%2FnVun8y0vg2%2BXdxjAxzMoxRds3MQNeJfCAQXlGVbTFYn64FRYQVWA4XHl0GlHV6P90D989I; " +
            "s9key=h7os71uqXd";

    @Bean("restTemplate")
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(new CookieInterceptor());
        return restTemplate;
    }

    private class CookieInterceptor implements ClientHttpRequestInterceptor {
        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            request.getHeaders().add("Cookie", cookie);
            return execution.execute(request, body);
        }
    }

}
